﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using Microsoft.Phone.BackgroundAudio;
using SverigesRadio.SverigesRadioApp.WindowsPhoneApplication.Commands;

namespace SverigesRadio.SverigesRadioApp.WindowsPhoneApplication.ViewModels
{
    public class PlayerViewModel 
    {
        public MainPageViewModel ParentPageViewModel { get; private set; }
        public DispatcherTimer bufferTimer;


        public PlayerViewModel(MainPageViewModel parentPageViewModel)
        {
            ParentPageViewModel = parentPageViewModel;
            BackgroundAudioPlayer.Instance.PlayStateChanged += PlayStateChanged;

            bufferTimer = new DispatcherTimer {Interval = new TimeSpan(0, 0, 0, 1)};
            bufferTimer.Tick += BufferTimerTick;

            //AudioPlaybackAgent.AudioPlayer player = new AudioPlaybackAgent.AudioPlayer();
        }

        void BufferTimerTick(object sender, EventArgs e)
        {
            CheckBufferSize();
        }

        private void CheckBufferSize()
        {
            if (!IsStreamedTrack)
                return;

            BufferSize = BackgroundAudioPlayer.Instance.BufferingProgress;

            bufferTimer.Interval = BufferSize >= 1 ? new TimeSpan(0, 0, 1) : new TimeSpan(0, 0, 0, 1);
        }


        void PlayStateChanged(object sender, EventArgs e)
        {
            CheckPlayState();
        }

        private void CheckPlayState()
        {
            switch (BackgroundAudioPlayer.Instance.PlayerState)
            {
                    //case Microsoft.Phone.BackgroundAudio.PlayState.Unknown:
                    //    break;
                case Microsoft.Phone.BackgroundAudio.PlayState.Stopped:
                    PlayState = "Spelar inte";
                    IsPlaying = false;
                    break;
                    //case Microsoft.Phone.BackgroundAudio.PlayState.Paused:
                    //    break;
                case Microsoft.Phone.BackgroundAudio.PlayState.Playing:
                    PlayState = "Spelar";
                    IsPlaying = true;
                    break;
                case Microsoft.Phone.BackgroundAudio.PlayState.BufferingStarted:
                    PlayState = string.Format("Buffrar: {0:0%}" + BackgroundAudioPlayer.Instance.BufferingProgress);
                    break;
                    //case Microsoft.Phone.BackgroundAudio.PlayState.BufferingStopped:
                    //    break;
                    //case Microsoft.Phone.BackgroundAudio.PlayState.TrackReady:
                    //    break;
                    //case Microsoft.Phone.BackgroundAudio.PlayState.TrackEnded:
                    //    break;
                    //case Microsoft.Phone.BackgroundAudio.PlayState.Rewinding:
                    //    break;
                    //case Microsoft.Phone.BackgroundAudio.PlayState.FastForwarding:
                    //    break;
                    //case Microsoft.Phone.BackgroundAudio.PlayState.Shutdown:
                    //    break;
                    //case Microsoft.Phone.BackgroundAudio.PlayState.Error:
                    //    break;
                default:
                    PlayState = "Spelar inte";
                    IsPlaying = false;
                    break;

            }
        }

        public ICommand LoadedCommand
        {
            get { return new Command(null, p => Loaded()); }
        }

        private void Loaded()
        {
            CheckPlayState();
            CheckBufferVisibility();
        }


        public ICommand PlayCommand
        {
            get { return new Command(null, p => Play()); }
        }

        private void Play()
        {
            BackgroundAudioPlayer.Instance.Track = SelectedAudioTrack;
            BackgroundAudioPlayer.Instance.Play();

            if (!IsStreamedTrack) return;

            BufferVisibility = Visibility.Visible;
            bufferTimer.Start();

        }

        private void CheckBufferVisibility()
        {
            BufferVisibility = IsStreamedTrack && IsPlaying ? Visibility.Visible : Visibility.Collapsed;

            CheckBufferSize();
        }

        public ICommand StopCommand
        {
            get { return new Command(null, p => Stop()); }
        } 

        //public ChannelInformationViewModel SelectedChannel
        //{
        //    get { return ParentPageViewModel.DirektControlViewModel.SelectedChannel; }
        //}

        private void Stop()
        {
            BackgroundAudioPlayer.Instance.Stop();
            BufferVisibility = Visibility.Collapsed;

        }


        private string playState;

        public string PlayState
        {
            get { return playState; }
            set
            {
                playState = value;

            }
        }

        private double bufferSize;

        public double BufferSize
        {
            get { return bufferSize; }
            set
            {
                bufferSize = value;

            }
        }

        private Visibility bufferVisibility;

        public Visibility BufferVisibility
        {
            get { return bufferVisibility; }
            set
            {
                bufferVisibility = value;

            }
        }

        private bool isPlaying;

        public bool IsPlaying
        {
            get { return isPlaying; }
            set
            {
                isPlaying = value;

            }
        }

        public bool IsPlayEnabled
        {
            get { return !IsPlaying; }
        }

        public AudioTrack SelectedAudioTrack
        {
            get;
            set;
        }

        private bool isStreamedTrack;
        public bool IsStreamedTrack
        {
            get { return isStreamedTrack; }
            set
            {
                isStreamedTrack = value;

            }
        }
    }
}
